angular.module('app.services', [])

.factory('communicationService', ['$http', '$q', function($http, $q) {

    function judgeResult(result, deferred) {
        deferred.resolve(result.data);
    }

    function get(url, params) {
        var deferred = $q.defer();
        $http.get(url, {params: params}).then(function(result) {
            judgeResult(result, deferred);
        }, function(err) {
            deferred.reject(err);
        });
        return deferred.promise;
    }

    function post(url, params) {
        var deferred = $q.defer();
        $http.post(url, params).then(function(result) {
            judgeResult(result, deferred);
        }, function(err) {
            deferred.reject(err);
        });
        return deferred.promise;
    }

    function put(url, params) {
        var deferred = $q.defer();
        $http.put(url, params).then(function(result) {
            judgeResult(result, deferred);
        }, function(err) {
            deferred.reject(err);
        });
    }

    function del(url, params) {
        var deferred = $q.defer();
        $http.delete(url, {params: params}).then(function(result) {
            judgeResult(result, deferred);
        }, function(err) {
            deferred.reject(err);
        });
    }

    return {
        get: get,
        post: post,
        put: put,
        delete: del
    }
}])
